/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author unnati
 */
@Table(name = "subjects")
@Entity
public class Subject extends AbstractEntity {

    @GeneratedValue

    @Column(name = "id")
    @Id
    protected Long id;

    @Column(name = "name")
    protected String name;

    @Column(name = "addedby")
    protected Long addedby;

    @Column(name = "lecturesperweek")
    protected int lecturesperweek;

    @Column(name = "labsperweek")
    protected int labsperweek;

    @Transient
    protected Admin admin;

//    @Transient
//    protected int type = 1;
    
    @Transient
    protected boolean type ;

    public boolean isType() {
        return type;
    }

    public void setType(boolean type) {
        this.type = type;
    }


    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin() {
        this.admin = (Admin) new Admin().getById(addedby);
    }

    public int getLecturesperweek() {
        return lecturesperweek;
    }

    public void setLecturesperweek(int lecturesperweek) {
        this.lecturesperweek = lecturesperweek;
    }

    public int getLabsperweek() {
        return labsperweek;
    }

    public void setLabsperweek(int labsperweek) {
        this.labsperweek = labsperweek;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAddedby() {
        return addedby;
    }

    public void setAddedby(Long addedby) {
        this.addedby = addedby;
    }

    public static List<Subject> getallsubject() {
        List<Subject> li = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();

            Query q = s.createQuery("from Subject");

            li = q.list();

        } catch (Exception e) {
        }
        return li;
    }

    public Subject GetByName() {
        Subject sub = null;

        Session s = HibernateUtil.getSessionFactory().openSession();
        Query q = s.createQuery("from Subject where name='" + this.name + "'");

        sub = (Subject) q.list().get(0);
        return sub;
    }

    public static List<Subject> getAllSubjectForBranchAndSemester(Long brid, int semester) {
        List<Subject> li = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();

            System.out.println("sql = from Subject where bid=" + brid + " AND semester=" + semester);
            Query q = s.createQuery("from Subject where bid='" + brid + "' AND semester=" + semester);

            li = q.list();
            System.out.println("Subject Arr Sizee=" + li.size());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return li;
    }

    public  static Subject getParameterForSubject(Long subid) {
        Subject sub = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();

            Query q = s.createQuery("from Subject where id=" + subid);
            System.out.println("from Subject where id=" + subid);
            sub = (Subject) q.list().get(0);
            System.out.println(sub);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sub;
    }

}
